package com.ruoyi.tasks.service.impl;

import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.tasks.domain.HouseholdTasks;
import com.ruoyi.tasks.dto.TaskDto;
import com.ruoyi.tasks.mapper.HouseholdTasksMapper;
import com.ruoyi.tasks.service.ICodeService;
import com.ruoyi.tasks.service.IHouseholdTasksService;
import com.ruoyi.tasks.util.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import static com.ruoyi.tasks.util.AESUtil.encrypt;

/**
 * @Author: Feng.Xiang
 * @Date 2022-10-07
 * @Description:
 * @Modified By:
 * @Version: 1.0
 */
@Service
public class ICodeServiceImpl implements ICodeService {

    @Autowired
    private HouseholdTasksMapper householdTasksMapper;

    @Override
    public void getCode() {
        // 使用AES加密工具
        String key = "5F6B2AK33DZE20A0";
        String userId = SecurityContextHolder.getUserId().toString();
        try {
            String userEnd = encrypt(userId, key);

            HouseholdTasks household = householdTasksMapper.getHousehold(SecurityContextHolder.getUserName());
            String id = household.getId().toString();

            String householdEnd = encrypt(id, key);

            // 生成二维码
            String content="http://localhost/task?user," + userEnd + ",task," + householdEnd;

            System.out.println(content);

            BufferedImage image = QRCodeUtil.createImage( "utf-8", content, 360, 360 );

            //QRCodeUtil.textToImage( "扫码获取答案", 100, 100, 10 );

            QRCodeUtil.addUpFont( image,"用户完成核酸" );

            String formatName="png";
            String imagePath="F:\\IntelliJIDEA\\projects\\RuoYi-Cloud\\ruoyi-ui\\src\\assets\\images\\code.png";

            ImageIO.write(image, formatName, new File(imagePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("执行完成");
    }
}
